#include<stdio.h>
#include"stack.h"
#include<stdlib.h>
#define true 1
#define false 0

int Initialize(struct Stack *array)
{
    array->stack = (ElementType *)malloc(sizeof(ElementType) * array->size);
    if(array->stack == NULL)
    {
        printf("malloc error!\n");
        return false;
    }
    return true;
}


int ReallocArray(struct Stack *array)
{
    ElementType *temp = array->stack;
    // array->size = array->size * 2;
    array->stack = (ElementType *)malloc(sizeof(ElementType) * array->size);
    if(array->stack == NULL)
    {
        printf("ReallocArray error!\n");
        array->stack = temp;
        array->size = array->size / 2;
        return false;
    }

    for(int i = 0; i < array->len; i++)
    {
        array->stack[i] = temp[i];
    }

    Free(temp);
    return true;
}

void push(struct Stack *s0,ElementType element)
{
    if(s0->len>=s0->size)
        if(ReallocArray(s0) == false)
        {
            printf("can not  contain more elements!\n");
            return false;
        }

    s0->stack[s0->len]=element;
    s0->len++;
}

ElementType pop(struct Stack *s0)
{
    if(s0->len==0)
    {
        printf("empty Stack!\n");
        // return;        
    }
    s0->len--;
    return s0->stack[s0->len];
}

void FreeStack(struct Stack *array)
{
    if(array->stack!=NULL)
    {
        free(array->stack);
        array->stack=NULL;
        array->len=0;
    }
}


// int main()
// {
//     struct Stack s0;
//     s0.len=0;

//     for(int i=0;i<5;i++)
//     {
//         push(&s0,i+1);
//     }

//     while(s0.len!=0)
//     {
//         printf("%d ",pop(&s0));
//     }
//     printf("\n");
//     return 0;
// }

